<?php

namespace common\models\datas;

use common\components\extenders\MenuManage;
use common\components\helpers\Fun;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use \zhtfrontend\models\datas\Admin;

/**
 * This is the model class for table "{{%customer_field_config}}".
 *
 * @property integer $id
 * @property string $code
 * @property integer $is_required
 * @property string $type
 * @property string $module
 * @property integer $admin_id
 * @property integer $created_at
 * @property integer $default_value
 * @property string $type_value
 * @property string $notes
 */
class CustomerFieldConfig extends \common\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%customer_field_config}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['is_required', 'data_type'], 'integer'],
            [['code','notes'], 'string', 'max' => 50],
            [['type', 'module', 'default_value'], 'string', 'max' => 20],
            [['type_value'], 'string', 'max' => 255],
            [['code', 'type', 'module', 'is_required'], 'required'],
            [['type_value'], 'required', 'when' => function () {
                return $this->type == 'select';
            }],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', 'ID'),
            'code' => Yii::t('zhtfrontend', '字段名'),
            'is_required' => Yii::t('zhtfrontend', '是否必填'),
            'type' => Yii::t('zhtfrontend', '类型'),
            'module' => Yii::t('zhtfrontend', '针对模块'),
            'admin_id' => Yii::t('zhtfrontend', '创建人'),
            'created_at' => Yii::t('zhtfrontend', '创建时间'),
            'type_value' => Yii::t('zhtfrontend', '类型值'),
            'data_type' => Yii::t('zhtfrontend', '数据类型'),
            'default_value' => Yii::t('zhtfrontend', '默认值'),
            'notes' => Yii::t('zhtfrontend', '字段描述'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['is_required'] = function () {
            switch ($this->is_required) {
                case 0:
                    return \Yii::t('zhtfrontend', '否');
                case 1:
                    return \Yii::t('zhtfrontend', '是');
            }
        };
        $fields['type'] = function () {
            switch ($this->type) {
                case 'text':
                    return \Yii::t('zhtfrontend', '文本框');
                case 'select':
                    return \Yii::t('zhtfrontend', '下拉框');
                case 'textarea':
                    return \Yii::t('zhtfrontend', '文本域');
            }
        };
        $fields['module'] = function () {
            switch ($this->module) {
                case 'customer':
                    return \Yii::t('zhtfrontend', '会员');
                case 'student':
                    return \Yii::t('zhtfrontend', '学员');
            }
        };
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };

        return $fields;
    }

    public function extraFields()
    {
        return [
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
            'admin' => function () {
                $admin = Admin::findOne($this->admin_id);
                return (!empty($admin)) ? $admin->username : '--';
            },
            'status' => function () {
                $relation = CustomerFieldConfigRelation::findOne(['customer_field_config_id' => $this->id, 'site_id' => Fun::getSiteId()]);
                if(!empty($relation)){
                    switch ($relation->status) {
                        case 1:
                            return \Yii::t('zhtfrontend', '启用');
                        case 2:
                            return \Yii::t('zhtfrontend', '禁用');
                    }
                }else{
                    return \Yii::t('zhtfrontend', '未启用');
                }

            },
        ];
    }

    public function getAdmin()
    {
        return $this->hasOne(Admin::className(), ['id' => 'admin_id', 'site_id' => Fun::getSiteId()]);
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
        ]);
    }
}
